import { _decorator, Component, Label, Layout, Node, Sprite, tween, Tween, UITransform } from 'cc';
import { NodeUtil } from '../Utils/NodeUtil';
import { PoolManager } from '../Managers/PoolManager';
import { ZTool } from '../Utils/ZTool';
const { ccclass, property } = _decorator;

@ccclass('HPBar')
export class HPBar extends Component {
    private Lv: Node;
    private LvBG: Node;
    private LvLabel: Label;

    private DelayBar_0: Sprite;
    private DelayBar_1: Sprite;
    private Bar_0: Sprite;
    private Bar_1: Sprite;

    Bar: Sprite;
    DelayBar: Sprite;
    Layout: Layout;
    Transform: UITransform;

    DividingLines: Node[] = [];

    DividingLineHeigh: number = 3;
    DividingLineCount: number = 0;
    divid: number = 10;

    protected onLoad(): void {
        this.DelayBar_0 = NodeUtil.GetComponent("DelayBar_0", this.node, Sprite);
        this.DelayBar_1 = NodeUtil.GetComponent("DelayBar_1", this.node, Sprite);
        this.Bar_0 = NodeUtil.GetComponent("Bar_0", this.node, Sprite);
        this.Bar_1 = NodeUtil.GetComponent("Bar_1", this.node, Sprite);

        this.Lv = NodeUtil.GetNode("Lv", this.node);
        this.LvBG = NodeUtil.GetNode("LvBG", this.node);
        this.LvLabel = NodeUtil.GetComponent("LvLabel", this.node, Label);
    }

    /**绿：0 红：1 */
    Init(type: number, lv: number, hp: number, divid: number = 20) {
        this.DelayBar_0.node.active = type == 0;
        this.DelayBar_1.node.active = type == 1;
        this.LvBG.getChildByName("0").active = type == 0;
        this.LvBG.getChildByName("1").active = type == 1;
        this.Bar_0.node.active = type == 0;
        this.Bar_1.node.active = type == 1;

        this.LvLabel.string = `${lv}`;

        this.Bar = type == 0 ? this.Bar_0 : this.Bar_1;
        this.DelayBar = type == 0 ? this.DelayBar_0 : this.DelayBar_1;
        this.Layout = this.Bar.getComponent(Layout);
        this.Transform = this.Bar.getComponent(UITransform);

        this.DividingLineCount = Math.floor(hp / divid) - 1;

        this.Layout.spacingX = (this.Transform.contentSize.width - this.DividingLineCount * this.DividingLineHeigh) / Math.floor(hp / divid);
        this.Layout.paddingLeft = (this.Transform.contentSize.width - this.DividingLineCount * this.DividingLineHeigh) / Math.floor(hp / divid);

        for (let i = 0; i < this.DividingLineCount; i++) {
            PoolManager.GetNode("Prefabs/UI/BloodBarDividingLine", this.Bar.node).then((node: Node) => {
                this.DividingLines.push(node);
            });
        }

        this.Bar.fillRange = 1;
        this.DelayBar.fillRange = 1;
    }

    Set(ratio: number) {
        ratio = ZTool.Clamp(ratio, 0, ratio);
        this.Bar.fillRange = ratio;
        let count = Math.ceil(ratio * this.DividingLineCount);

        if (this.Bar.node.children.length > count) {
            let needPutCount = this.Bar.node.children.length - count;
            for (let i = 0; i < needPutCount; i++) {
                PoolManager.PutNode(this.Bar.node.children[0]);
            }
        }

        Tween.stopAllByTarget(this.DelayBar);
        this.scheduleOnce(() => {
            tween(this.DelayBar).to(0.5, { fillRange: ratio }, { easing: `quartIn` }).start();
        }, 0.75);
    }

}